package lesson4

import (
	"fmt"
	"strconv"
)

// Put向数据缓冲区存放数据
func (dq *DataQueue) Put(in interface{}) {
	//获取写锁
	//dq.WriteLock.Lock()
	//defer dq.WriteLock.Unlock()

	dq.FullCon.L.Lock()
	defer dq.FullCon.L.Unlock()
	// 判断缓冲区是否已满
	for (dq.WritePoint+1)%dq.Cap == dq.ReadPoint {
		//缓冲区满，则进入等待
		dq.FullCon.Wait()
	}
	// 赋值并移动指针
	fmt.Printf("Push DataQueue index: %d\n", dq.WritePoint)
	dq.Data[dq.WritePoint] = in.(string) + strconv.Itoa(dq.WritePoint)
	dq.WritePoint = (dq.WritePoint + 1) % dq.Cap
	// 通知缓冲区已有数据
	dq.EmptyCon.Signal()
}
